package com.shop.order.controller;

import com.shop.constant.Const;
import com.shop.order.service.OrderService;
import com.shop.pojo.LdbzCart;
import com.shop.utils.CookieUtils;
import com.shop.utils.FastJsonConvert;
import com.shop.utils.IDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/order/service")
@RestController
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderService orderService;

    @RequestMapping("/order/getOrderInfo")
    public Model showOrder(String ids, String nums, Model model, HttpServletResponse response, HttpServletRequest request) {
        String cookieValue = CookieUtils.getCookieValue(request, Const.TOKEN_LOGIN);

        int totalPrices = 0;
        model.addAttribute("totalPrices",totalPrices);
        return model;
    }

    @RequestMapping("/order/getPay")
    public String getPay(String orderItemCodes , String orderNums , String address , float shippingPrice , HttpServletResponse response, HttpServletRequest request) {

        //获取cookie购物车中的商品
        String cartCookieValue = CookieUtils.getCookieValue(request, Const.CART_KEY);
        String itemsJSON = new String(Base64Utils.decodeFromString(cartCookieValue));
        List<LdbzCart> cookieItems = FastJsonConvert.convertJSONToArray(itemsJSON, LdbzCart.class);
        //获取当前在线人员
        String userCookieValue = CookieUtils.getCookieValue(request, Const.TOKEN_LOGIN);

        orderService.generateOrder(userCookieValue,cookieItems,orderItemCodes, orderNums, address, shippingPrice);
        return "success";
    }
}
